<template>
  <div class="enterpriseInfo">
    <div>
      <div class="title">
        <h1 style="color: #337ECC;font-size: 40px">宣讲会信息</h1>
        <div>
          <CopyOutlined class="icon"/>
          <TagOutlined class="icon"/>
        </div>


      </div>
    </div>
    <div style="border: 2px solid #337ECC; padding: 20px;border-top: 30px solid #337ECC; border-radius: 10px">

      <a-form
          ref="formRef"
          :label-col="labelCol"
          :model="formState"
          :rules="rules"
          :wrapper-col="wrapperCol"
      >



        <a-divider orientation="left" style="color:#337ECC; font-size: 20px">基本信息</a-divider>
        <div class="basic">
          <div class="basicInfo">
            <a-form-item ref="videoName" label="宣讲会-名称" name="videoName" style="width: 300px">
              <a-input v-model:value="formState.videoName" :bordered="isBordered" :disabled="true"/>
            </a-form-item>
            <a-form-item ref="videoCompanyName" label="公司名称" name="videoCompanyName" style="width: 300px">
              <a-input v-model:value="formState.videoCompanyName" :bordered="isBordered" :disabled="true"/>
            </a-form-item>
            <a-form-item ref="videoStartTime" :wrapperCol="{ span: 12 }" label="开始时间"
                         name="videoStartTime">
              <a-date-picker
                  :bordered="isBordered" :disabled="true"
                  style="width: 300px"
                  show-time
                  v-model:value="formState.videoStartTime"
              />
            </a-form-item>

            <a-form-item ref="videoEndTime" :wrapperCol="{ span: 12 }" label="结束时间"
                         name="videoEndTime">
              <a-date-picker
                  :bordered="isBordered" :disabled="true"
                  style="width: 300px"
                  show-time
                  v-model:value="formState.videoEndTime"
              />
            </a-form-item>

            <a-form-item ref="videoRemarks" label="补充信息"
                         name="videoRemarks">
              <a-textarea v-model:value="formState.videoRemarks"
                          :auto-size="{minRows: 3, maxRows: 20}"
                          show-count
                          :bordered="isBordered" :disabled="true"
                          style="width: 500px"
              />
            </a-form-item>

          </div>

          <div class="update">
            <a-upload
                v-model:file-list="fileList"
                :before-upload="beforeUpload"
                :disabled="!isBordered"
                :show-upload-list="false"
                action="http://localhost:8080/file/upload"
                class="avatar-uploader"
                list-type="picture-card"
                name="file"
                style="width: 100%; height: 100%;"
                @change="handleChange"
            >
              <img v-if="imageUrl" :src="imageUrl" alt="avatar" style="width: 100%; height: 100%; border-radius: 50%"/>
              <div v-else>
                <loading-outlined v-if="loading"></loading-outlined>
                <plus-outlined v-else></plus-outlined>
                <div class="ant-upload-text">上传LOGO</div>
              </div>
            </a-upload>
          </div>
        </div>
        <div class="school">
          <a-divider orientation="left" style="color:#337ECC; font-size: 20px">视频信息</a-divider>
          <a-form-item ref="videoUrl" label="宣讲会视频" name="videoUrl">
            <video
                ref="videoRef"
                :autoplay="false"
                :src="'http://localhost:8080/video-file/video?videoName='+formState.videoUrl"
                controls
                height="400"
                style="margin-right: 10px; border: 1px solid red"
                width="auto"
                @timeupdate="handleTimeUpdate"
            ></video>
          </a-form-item>
        </div>
      </a-form>
    </div>
  </div>
</template>
<script setup>
import {CopyOutlined, SmileOutlined, TagOutlined} from "@ant-design/icons-vue";
import {computed, h, onMounted, reactive, ref, toRaw} from 'vue';
import {message, notification} from "ant-design-vue";
import moment from "moment";
import {useRoute, useRouter} from "vue-router";
import {queryVideoByEnterpriseUserIdAIP, queryVideoDetailByVideoIdAPI, updateVideoByIdAPI} from "@/apis/video.js";
import dayjs from "dayjs";

const rules = {
  videoCertificationRefuse: [
    {
      required: true,
      message: '请输入不通过原因！',
      trigger: 'change',
    },
  ],
}
const formRef = ref();
const formState = reactive({
  videoStartTime: null,
  videoEndTime: null,
  videoRemarks:'',
  videoName: '',
  videoCertificationStatus: '',
  videoCertificationRefuse: '',
  videoUrl: '',
  videoStatus: '',
  videoCompanyName: '',
  videoId:''
});

const success = (msg) => {
  message.success(msg);
};
//审核传递的参数
const data = ref({videoId: '', videoCertificationStatus: '', videoCertificationRefuse: ''})
//审核通过
const auditYes = async () => {
  data.videoCertificationStatus = "审核通过"
  data.videoId = videoId.value
  const res = await updateVideoByIdAPI(data);
  if (res.data.flag) {
    success("审核成功");
    // 设置一个定时器，在1秒后刷新页面
    setTimeout(() => {
      router.go(-1); // -1 表示返回上一个页面
    }, 1000); // 1000毫秒 = 1秒
  } else {
    console.log('error submit!', toRaw(formState));
  }
};
//审核不通过
const auditNo = async () => {
  data.videoCertificationStatus = "审核不通过"
  data.videoId = videoId.value
  data.videoCertificationRefuse = formState.videoCertificationRefuse
  const res = await updateVideoByIdAPI(data);
  if (res.data.flag) {
    success("审核成功");
    // 设置一个定时器，在1秒后刷新页面
    setTimeout(() => {
      router.go(-1); // -1 表示返回上一个页面
    }, 1000); // 1000毫秒 = 1秒
  } else {
    console.log('error submit!', toRaw(formState));
  }
};
//重置后是否刷新
const isRefresh = ref(false);


//控制按钮
const isSave = ref();
const isUpdate = ref(false);//是否修改
const isBordered = ref(false);//是否边框
//获取数据
//获取当前登录用户信息
//获取数据
const getData = async (videoId) => {
  const res = await queryVideoDetailByVideoIdAPI(videoId)
  if (res.data.data == null) {
    isSave.value = true;
  } else {
    isSave.value = false;
  }

  formState.videoName = res.data.data.videoName;
  formState.videoCertificationStatus = res.data.data.videoCertificationStatus;
  formState.videoUrl = res.data.data.videoUrl;
  formState.videoStartTime = dayjs(res.data.data.videoStartTime);
  formState.videoEndTime = dayjs(res.data.data.videoEndTime);
  formState.videoRemarks = res.data.data.videoRemarks;
  formState.videoStatus = res.data.data.videoStatus;
  formState.videoCompanyName = res.data.data.videoCompanyName;
  formState.enterprise_foundation_date = moment(res.data.data.enterpriseFoundationDate, 'YYYY-MM-DD');
  formState.enterprise_type = res.data.data.enterpriseType;
  formState.enterprise_address = res.data.data.enterpriseAddress;
  formState.enterprise_email = res.data.data.enterpriseEmail;
  formState.enterprise_website = res.data.data.enterpriseWebsite;
  formState.enterprise_social_media_links = res.data.data.enterpriseSocialMediaLinks;
  formState.enterprise_logo_url = res.data.data.enterpriseLogoUrl;

  //获取图片
  imageUrl.value = "http://localhost:8080/file/download?name=" + res.data.data.videoCompanyLogo;
  console.log(imageUrl.value)

}

//接收路由参数
const route = useRoute()
const videoId = ref();
//生命周期
onMounted(() => {

  videoId.value = route.params.videoId;
  formState.videoId = videoId.value;
  getData(videoId.value);

});

//上传照片
function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

const fileList = ref([]);
const loading = ref(false);
const imageUrl = ref('');
const handleChange = info => {
  if (info.file.status === 'uploading') {
    loading.value = true;
    return;
  }
  if (info.file.status === 'done') {
    // Get this url from response in real world.
    getBase64(info.file.originFileObj, base64Url => {
      imageUrl.value = base64Url;
      loading.value = false;
    });
    formState.enterprise_logo_url = fileList.value[0].response.data;
    console.log(formState.enterprise_logo_url)

  }
  if (info.file.status === 'error') {
    loading.value = false;
    message.error('upload error');
  }
};
const beforeUpload = file => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('You can only upload JPG file!');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('Image must smaller than 2MB!');
  }
  return isJpgOrPng && isLt2M;
};

const openNotification = () => {
  notification.open({
    message: '温馨提示！！！',
    description:
        '点击输入框或照片均可进行修改',
    icon: () =>
        h(SmileOutlined, {
          style: 'color: red',
        }),
  });
};

const router = useRouter();
const goBack = () => {
  router.go(-1); // -1 表示返回上一个页面
}
const cancel = () => {
  isSave.value = false;
  isUpdate.value = false;
  isBordered.value = false;
  if (isRefresh.value) {
    // 设置一个定时器，1秒（1000毫秒）后执行页面刷新
    setTimeout(function () {
      location.reload(); // 刷新当前页面
    }, 500);
  }
}


//审核模块
const ghost = ref(false);

//时间格式处理
const formattedFoundationDate = computed(() => {
  const date = new Date(formState.enterprise_foundation_date);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1
  const day = String(date.getDate()).padStart(2, '0');
  // const hours = String(date.getHours()).padStart(2, '0');
  // const minutes = String(date.getMinutes()).padStart(2, '0');
  // const seconds = String(date.getSeconds()).padStart(2, '0');
  // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  return `${year}-${month}-${day}`;
});

//拒绝通知框
const open = ref(false);
const showModal = () => {
  open.value = true;
};
const handleOk = e => {
  console.log(e);
  auditNo(); // 调用 auditNo 方法
  open.value = false;
};


</script>
<style lang="scss" scoped>

.enterpriseInfo {
  margin: 5px;
  padding: 20px 60px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1), 0 6px 20px rgba(0, 0, 0, 0.1);
  border-radius: 30px; /* 你可以调整这个值来改变圆角的大小 */

  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    //border-bottom: 15px solid #4D7181;

    .icon {
      font-size: 35px;
      padding: 15px;
      color: white;
      margin-right: 20px;
      background-color: #C09E66;
      border-radius: 50px;
      margin-bottom: 10px;
    }
  }

  .basic {
    display: flex;
    padding-left: 30px;
    justify-content: space-between;

    .basicInfo {
      width: 70%;
      height: 100%;
      display: flex;
      flex-wrap: wrap; /* 允许子元素换行 */
      gap: 0 30px; /* 设置水平间距为10px，垂直间距为0 */
    }

    .update {
      margin-right: 100px;
    }
  }

  .school {
    width: 100%;
    padding-left: 30px;
    margin-top: 50px;
    //justify-content: space-between;
    display: flex;

    flex-wrap: wrap; /* 允许子元素换行 */
    gap: 0 30px; /* 设置水平间距为10px，垂直间距为0 */
  }

  .update {
    margin-right: 100px;
    width: 170px;
    height: 170px;
    border-radius: 50%;
    //padding: 33px;
    //border: 1px solid #ccc;
  }

  :deep(.ant-upload-wrapper .ant-upload-disabled) {
    height: 170px;
    width: 170px;
    border: 2px solid #ccc;
    border-radius: 50%;
  }

  :deep(.ant-upload .ant-upload-disabled) {
    height: 170px;
    width: 170px;
    border: 2px solid #ccc;
    border-radius: 50%;
  }

  :deep(.ant-upload-wrapper.ant-upload-picture-card-wrapper .ant-upload.ant-upload-select) {
    height: 170px;
    width: 170px;
    border: 2px solid #ccc;
    border-radius: 50%;
  }

  :deep(.ant-upload .ant-upload-disabled) {
    height: 170px;
    width: 170px;
    border: 2px solid #ccc;
    border-radius: 50%;
  }
}


//.ant-upload-select-picture-card i {
//  font-size: 32px;
//  color: #999;
//}
//
//.ant-upload-select-picture-card .ant-upload-text {
//  margin-top: 8px;
//  color: #666;
//}
</style>
